(ns ura.migrations.migrations
  (:require
   [migratus.core :as migratus]
   [next.jdbc :as jdbc]
   [ura.config :as cfg]
   [taoensso.telemere :as t]
   )
  )

(defn initMigrateConfig [cf]
  (cfg/loadConfig cf)
  (def config {:store                :database
               :migration-dir        "ura/migrations/"
               :init-script          "init.sql" ;script should be located in the :migration-dir path
                                        ;defaults to true, some databases do not support
                                        ;schema initialization in a transaction
               :init-in-transaction? false
               :migration-table-name "_migration_info"
               :db {
                    :connection (jdbc/get-connection (:dm cfg/cfg))
                    :managed-connection? true
                    }
               }
    )
  )

(defn migrate [& args]
  (t/log! ["migrate data " args])
  (initMigrateConfig (first args))
  (migratus/migrate config)
  )

(defn rollback [& args]
  (initMigrateConfig (first args))
  (migratus/rollback config)
  )

(defn -main [& args]
  (case (first args)
        "migrate" (migrate (second args))
        "rollback" (rollback (second args))
        (println "invalid param " (first args))
        )
  )


